package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.SysSubject;
import com.example.demo.entity.SysTitleSubmitRecord;
import com.example.demo.mapper.SysTitleSubmitRecordMapper;
import com.example.demo.service.SysTitleSubmitRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.UserInfoUtil;
import com.example.demo.utils.enu.SubmissionEnum;
import com.example.demo.utils.enu.UserInfoEnu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ld&zjw
 * @since 2024-01-31
 */
@Service
public class SysTitleSubmitRecordServiceImpl extends ServiceImpl<SysTitleSubmitRecordMapper, SysTitleSubmitRecord> implements SysTitleSubmitRecordService {

    @Resource
    SysTitleSubmitRecordMapper sysTitleSubmitRecordMapper;

    @Override
    public Page<SysTitleSubmitRecord> getById(Long id) {
        LambdaQueryWrapper<SysTitleSubmitRecord> wrapper = Wrappers.<SysTitleSubmitRecord>lambdaQuery();
        wrapper.eq(SysTitleSubmitRecord::getId,id);
        Page<SysTitleSubmitRecord> newsPage = sysTitleSubmitRecordMapper.selectPage(new Page<>(1, 1), wrapper);
        for (int i=0; i<newsPage.getRecords().size(); i++) {
            if (!UserInfoUtil.get(UserInfoEnu.username.getCode()).equals(newsPage.getRecords().get(i).getSubmitName())) {
                newsPage.getRecords().get(i).setSourceCode(null);
            }
        }
        return newsPage;
    }

    @Override
    public Page<SysTitleSubmitRecord> findRankingPage(Integer pageNum, Integer pageSize, String search){
        //排名页面统计
        LambdaQueryWrapper<SysTitleSubmitRecord> wrapper = new QueryWrapper<SysTitleSubmitRecord>().select(
                "submit_name,submit_nick,COUNT( CASE title_result WHEN '3' THEN 1 ELSE NULL END ) AS acceptNum, COUNT(1) AS submitNum"
        ).lambda();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(SysTitleSubmitRecord::getSubmitName,search);
        }
        wrapper.groupBy(SysTitleSubmitRecord::getSubmitName,SysTitleSubmitRecord::getSubmitNick);
        Page<SysTitleSubmitRecord> newsPage = sysTitleSubmitRecordMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        int passNum, submitNum;
        double result;
        for (int i=0; i<newsPage.getRecords().size(); i++){
            passNum = newsPage.getRecords().get(i).getAcceptNum();
            submitNum = newsPage.getRecords().get(i).getSubmitNum();
            result = (double) passNum/submitNum*100;
            DecimalFormat decimalFormat = new DecimalFormat("#0.00");
            newsPage.getRecords().get(i).setPassingRate(decimalFormat.format(result));
        }
        return newsPage;
    }

    @Override
    public Page<SysTitleSubmitRecord> findStatusPage(Integer pageNum, Integer pageSize, String search){

        LambdaQueryWrapper<SysTitleSubmitRecord> wrapper = Wrappers.<SysTitleSubmitRecord>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(SysTitleSubmitRecord::getTitleId,search);
        }
        wrapper.orderByDesc(SysTitleSubmitRecord::getTitleTime);
        Page<SysTitleSubmitRecord> newsPage = sysTitleSubmitRecordMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        for (int i=0; i<newsPage.getRecords().size(); i++){
            switch (newsPage.getRecords().get(i).getTitleResult()){
                case "1" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.In_Queue.getDesc());
                    break;
                case "2" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Processing.getDesc());
                    break;
                case "3" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Accepted.getDesc());
                    break;
                case "4" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Wrong_Answer.getDesc());
                    break;
                case "5" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Time_Limit_Exceeded.getDesc());
                    break;
                case "6" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Compilation_Error.getDesc());
                    break;
                case "7" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Runtime_Error_SIGSEGV.getDesc());
                    break;
                case "8" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Runtime_Error_SIGXFSZ.getDesc());
                    break;
                case "9" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Runtime_Error_SIGFPE.getDesc());
                    break;
                case "10" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Runtime_Error_SIGABRT.getDesc());
                    break;
                case "11" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Runtime_Error_NZEC.getDesc());
                    break;
                case "12" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Runtime_Error_Other.getDesc());
                    break;
                case "13" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Internal_Error.getDesc());
                    break;
                case "14" :
                    newsPage.getRecords().get(i).setTitleResult(SubmissionEnum.Exec_Format_Error.getDesc());
                    break;
            }
            if (!UserInfoUtil.get(UserInfoEnu.username.getCode()).equals(newsPage.getRecords().get(i).getSubmitName())){
                newsPage.getRecords().get(i).setSourceCode(null);
            }
        }
        return newsPage;
    }

}
